const momentService = require('../service/moment.service')

class MomentController {
  async create (ctx, next) {
    const userId = ctx.user.id;
    const content = ctx.request.body.content;

    // 插入数据库
    const result = await momentService.create(userId, content);
    ctx.body = result
  }
  async detail(ctx, next) {
    const momentId = ctx.params.momentId;

    // 根据id去查询这条数据
    const result = await momentService.getMomentById(momentId)
    ctx.body = result
  }
  async list (ctx, next) {
    // 1、获取数据(offset / size)
    const {offset , size} = ctx.query;

    const result = await momentService.getMomentList(offset, size);
    ctx.body =result
  }
  async update(ctx, next) {
    const {momentId} = ctx.params;
    const {content} = ctx.request.body;

    const result =await momentService.update(content, momentId);
    ctx.body = result
  }
  async remove(ctx, next) {
    const {momentId} = ctx.params;

    const result = await momentService.remove(momentId);
    ctx.body = result
  }
  async addLabels(ctx, next){
    const {labels} = ctx;
    const {momentId} = ctx.params;

    console.log(labels);
    for(let label of labels) {
      // 2.1判断标签是否已经和动态有联系
      const isExist = await momentService.hasLabel(momentId, label.id);
      if(!isExist){
        await momentService.addLabel(momentId,label.id)
      }
    }
    ctx.body = "给动态添加标签成功~"
  }

}

module.exports =new MomentController()